//
//  ITErrorTipView.m
//  Water
//
//  Created by seven on 2020/8/8.
//  Copyright © 2020 seven. All rights reserved.
//

#import "ITErrorTipView.h"
@interface ITErrorTipView(){
    BaseLabel *_tipLbl;
    BaseImageView *_icon;
}
@end
@implementation ITErrorTipView

+ (instancetype)showWithAction:(CustomAction)action{
    ITErrorTipView *view = [super showWithAction:action];
    [view setTitle:nil des:nil actionTitle:nil];
    return view;
}



+ (instancetype)showWithTitle:(NSString *)title des:(NSString *)des  actionTitle:(NSString *)actionTitle   Action:(CustomAction)action{
    ITErrorTipView *view = [self showWithAction:action];
    [view setTitle:title des:des actionTitle:actionTitle];
    return view;
}

- (void)initViews{
    [super initViews];
    
    self.title = LocalTitle(@"Sorry");
    
    _tipLbl = [BaseLabel initWithFrame:CGRectMake(18, 86, self.contentView.width-36, 100)];
    _tipLbl.numberOfLines = 0;
    _tipLbl.font = SemiboldFontWithSize(16);
    _tipLbl.textColor = COLOR_333333;
    _tipLbl.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_tipLbl];
    
    UIButton *okBtn = self.okBtn;
    okBtn.width = 196;
    okBtn.centerX = self.contentView.width/2;
}


- (void)setTitle:(NSString *)title des:(NSString *)des  actionTitle:(NSString *)actionTitle{
    if (title.length == 0) {
        title = LocalTitle(@"Sorry");
    }
    
    self.title = title;
    
    if (des.length == 0) {
        des = LocalTitle(@"Playback failed. Please try again.");
    }
    if (actionTitle.length == 0) {
        actionTitle = LocalTitle(@"Once Again");
    }
    
    NSMutableAttributedString *tmp = nil;
    if ([des hasPrefix:@"<font"]) {
        tmp = [NSAttributedString generateAttriuteStringWithHtml:des];
    }else {
        tmp = [NSAttributedString generateAttriuteStringWithStr:des WithColor:_tipLbl.textColor WithFont:_tipLbl.font];
    }
    tmp.lineSpacing = 4;
    tmp.alignment = NSTextAlignmentCenter;
    _tipLbl.attributedText = tmp;
    
    [_tipLbl fitWidth:_tipLbl.width];

    [self.okBtn setTitleNormal:actionTitle];
    
    self.contentHeight = _tipLbl.bottom+86;
}

- (void)setShowInfo:(NSDictionary *)showInfo{
    _showInfo = showInfo;
    
    NSString *title = _showInfo[@"title"];
    NSString *icon = _showInfo[@"icon"];
    NSString *desc = _showInfo[@"desc"];
    NSString *btnText = _showInfo[@"btnText"];
    
    if (icon.length > 0) {
        _icon = [BaseImageView initWithFrame:CGRectMake(_tipLbl.left, _tipLbl.top, _tipLbl.width, 100)];
        [self.contentView addSubview:_icon];
        [_icon setImageWithUrl:icon];
        _tipLbl.top = _icon.bottom + 10;
    }
    [self setTitle:title des:desc actionTitle:btnText];
    
}

@end
